package com.memorypalace.deck.biz;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.memorypalace.deck.mapper.CardMapper;
import com.memorypalace.deck.mapper.DeckMapper;
import com.memorypalace.deck.mapper.NoteMapper;
import com.memorypalace.deck.mapper.NoteTypeMapper;
import com.memorypalace.deck.vo.NoteQueryPageBean;
import com.memorypalace.knowledge.mapper.NoteConnectMapper;
import com.memorypalace.knowledge.pojo.NoteConnect;
import com.memorypalace.pojo.Card;
import com.memorypalace.pojo.CardStyle;
import com.memorypalace.pojo.Deck;
import com.memorypalace.pojo.Note;
import com.memorypalace.pojo.NoteType;
import com.memorypalace.study.mapper.CardStudyRecordMapper;
import com.memorypalace.study.mapper.CardStyleMapper;

@Service("noteBiz")
public class NoteBiz implements INoteBiz {

	@Autowired
	private DeckMapper deckMapper;
	
	@Autowired
	private NoteMapper noteMapper;
	@Autowired
	private NoteConnectMapper noteConnectMapper;
	@Autowired
	private NoteTypeMapper noteTypeMapper;
	
	@Autowired
	private CardMapper cardMapper;
	@Autowired
	private CardStyleMapper cardStyleMapper;
	@Autowired
	private CardStudyRecordMapper cardStudyRecordMapper;
	
	@Override
	public List<Note> getNotesByUserId(String userId) { 
		List<Deck> deckList = deckMapper.getDecksByUserId(userId);
		List<Note> noteList = new ArrayList<Note>();
		for (Deck deck : deckList) {
			noteList.addAll(noteMapper.getNotesByDeckId(deck.getDeckId()));
		}
		return noteList;
	}

	@Override
	public List<Note> getNotesByDeckId(String deckId) {
		return noteMapper.getNotesByDeckId(deckId);
	}

	@Override
	public NoteQueryPageBean getNoteQueryPageBeanByNoteQueryPageBean(NoteQueryPageBean noteQueryPageBean) {
		NoteQueryPageBean result =  noteMapper.getNoteQueryPageBeanByNoteQueryPageBean(noteQueryPageBean);
		if(result == null){
			return null;
		}
		result.setRecordNum(noteMapper.getCountByNoteQueryPageBean(noteQueryPageBean));
		return result;
	}
	
	@Override
	/**
	 * @prama noteQueryPageBean: 外面设置curPage
	 */
	public NoteQueryPageBean getNoteQueryPageBeanByUserId(String userId,NoteQueryPageBean noteQueryPageBean) {
		noteQueryPageBean.setUserId(userId);
		NoteQueryPageBean result =  noteMapper.getNoteQueryPageBeanByUserIdWithNoteQueryPageBean(noteQueryPageBean);
		if(result == null){
			return null;
		}
		result.setRecordNum(noteMapper.getCountByUserId(noteQueryPageBean));
		return result;
	}

	

	@Override
	public Note getNoteByNoteId(String noteId) {
		return noteMapper.getNoteWithDeckAndNoteTypeById(noteId);
	}
	
	

	@Override
	public Integer updateNote(Note note) {
		Note noteInDB = noteMapper.getNoteWithDeckAndNoteTypeById(note.getNoteId());
		if(!noteInDB.getNoteTypeId().equals(note.getNoteTypeId())){
			// 如果修改了note的noteType,则需找到之前的card然后删除card,cardStudyRecord
			List<Card> cardList = cardMapper.getCardByNoteId(note.getNoteId());
			for (Card card : cardList) {
				cardStudyRecordMapper.deleteCardStudyRecordByCardId(card.getCardId());
				cardMapper.deleteCardByCardId(card.getCardId());
			}
			
		}
		NoteType noteType = noteTypeMapper.getNoteTypeWithCardStyleByNoteTypeId(note.getNoteTypeId());
		List<CardStyle> cardStyleList = noteType.getCardStyleList();
		for (CardStyle cardStyle : cardStyleList) {
		}
		return noteMapper.updateNote(note);
	}

	

	@Override
	public Boolean addNote(Note note) {
		// 1.添加笔记
		Boolean result = noteMapper.addNote(note);
		// 2.拿到笔记类型
		NoteType noteType = noteTypeMapper.getNoteTypeWithCardStyleByNoteTypeId(note.getNoteTypeId());
		// 2.添加卡片
		for (CardStyle cardStyle : noteType.getCardStyleList()) {
			Card card = new Card(null, null, note.getNoteId(), 100, 0, 0, cardStyle.getCardStyleId());
			cardMapper.addCard(card);
		}
		return result;
	}

	

	

	@Override
	public Boolean deleteNote(String noteId) {
		// 1.删除与自己关联的卡片
		//拿到与自己关联的卡片
		List<Card> cardList = cardMapper.getCardByNoteId(noteId);
		for (Card card : cardList) {
			cardStudyRecordMapper.deleteCardStudyRecordByCardId(card.getCardId());
			cardMapper.deleteCardByCardId(card.getCardId());
		}
		// 2.删除自己的noteConnect
		noteConnectMapper.deleteNoteConnectByNoteId(noteId);
		// 3.把连接到自己的noteConnect设置为null
		noteConnectMapper.deleteNoteConnectByPointToNoteId(noteId);
		// 4.删除自己
		return noteMapper.deleteNote(noteId);
	}
	
	
	

}
